<?php echo $this->element('displayHeader'); ?>
<?php echo $ajax->form('ItsUsers', 'post', array('update'=>'content', 'indicator' => 'indicator', 'url'=>array('action'=>'save'))); ?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
            <?php echo $this->element('toolbar', array('buttons' => array('save', 'clear', 'back'))); ?>
        <?php else : // mode edit ?>
            <?php echo $this->element('toolbar', array('buttons' => array('save', 'add', array('refresh_view' => array('model' => 'User')),
                    array('delete' => array('model' => 'User', 'title' => 'name')),
                    array('edit' => array('text' => 'Change Password', 'action' => 'change_password', 'params' => $this->data['User']['id'])), 'back'))); ?>
        <?php endif; ?>
    </div>
    <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'User', 'title' => 'name')); ?></div>
</div>

<fieldset>
    <legend>User Information</legend>
    <table class="left" style="width: auto;">
        <tbody>
            <tr>
                <?php echo $form->input("User.id"); ?>
                <td width="150px"><?php echo $form->label("User.username", "Username", array('class' => 'required')) ?></td>
                <?php if($mode == 'add') : ?>
                <td><?php echo $form->text("User.username", array('size' => '30')) ?></td>
                <?php else: ?>
                <td><span><?php echo $this->data['User']['username'] ?></span></td>
                <?php endif; ?>
            </tr>

            <tr>
                <?php if($mode == 'add') : ?>
                <td><?php echo $form->label("User.password", "Password", array('class' => 'required')) ?></td>
                <td><?php echo $form->password("User.password", array('size' => '30')) ?> </td>
                <?php endif; ?>
            </tr>
            <tr>
                <?php if($mode == 'add') : ?>
                <td><?php echo $form->label("User.password_confirm", "Confirm Password", array('class' => 'required')) ?></td>
                <td><?php echo $form->password("User.password_confirm", array('size' => '30')) ?></td>
                <?php endif; ?>
            </tr>
            <tr>
                <td><?php echo $form->label("User.name", "Name", array('class' => 'required')) ?></td>
                <td><?php echo $form->text("User.name", array('size' => '30')) ?> </td>
            </tr>
            <tr>
                <td><?php echo $form->label("User.surname", "Surname", array('class' => 'required')) ?></td>
                <td><?php echo $form->text("User.surname", array('size' => '30')) ?> </td>
            </tr>
            <tr>
                <td><?php echo $form->label("User.email", 'Email') ?></td>
                <td><?php echo $form->text("User.email", array('size' => '30')) ?> </td>
            </tr>
            <tr>
                <td><?php echo $form->label("User.tel", 'Telephone') ?></td>
                <td><?php echo $form->text("User.tel", array('size' => '30')) ?> </td>
            </tr>
            <tr>
                <td><?php echo $form->label("User.birth_date", 'Birth date') ?></td>
                <td><?php echo $form->text("User.birth_date") ?></td>
            </tr>
            <?php if ($mode == 'edit') : ?>
            <tr>
                <td><?php echo $form->label("User.discontinued",'Discontinued') ?></td>
                <td><?php echo $form->checkbox('User.discontinued'); ?> Yes/No</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
    <fieldset class="left" style="width: auto;">
        <legend>User Type</legend>
        <?php echo $form->radio('User.prefix', array('its' => 'ITS', 'customer' => 'Customer', 'supplier' => 'Vendor'), array('separator' => '<br />', 'legend' => false)); ?>
    </fieldset>
</fieldset>
<fieldset id="fieldsetIts">
    <legend>ITS User Privileges</legend>
    <table>
        <tr>
            <td width="150px"><?php echo $form->label('UserIts.special_user', 'Special User'); ?></td>
            <td>
                <?php echo $form->input("UserIts.id"); ?>
                <?php
                // ถ้าเป็น Special user จะกำหนดสิทธิ์ให้ User อื่น ๆ ได้
                $special_user = $session->read('Auth.User.special_user');
                if ($special_user == true) : ?>
                    <?php echo $form->checkbox('UserIts.special_user'); ?> Yes/No
                <?php else: ?>
                    <?php echo 'No'; ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('UserIts.read_message', 'Read Message'); ?></td>
            <td>
                <?php if ($special_user == true) : ?>
                    <?php echo $form->checkbox('UserIts.read_message'); ?> Yes/No
                <?php else: ?>
                    <?php echo !empty($this->data['UserIts']) && $this->data['UserIts']['read_message'] == true? 'Yes': 'No'; ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('UserIts.its_report', 'Reporting'); ?></td>
            <td>
                <?php if ($special_user == true) : ?>
                    <?php echo $form->checkbox('UserIts.its_report'); ?> Yes/No
                <?php else: ?>
                    <?php echo !empty($this->data['UserIts']) && $this->data['UserIts']['its_report'] == true? 'Yes': 'No'; ?>
                <?php endif; ?>
            </td>
        </tr>
    </table>
</fieldset>
<fieldset id="fieldsetSupplier">
    <legend>Vendor User Privileges</legend>
    <table>
        <tr>
            <td width="150px"><?php echo $form->label("UserSupplier.supplier_id", "Vendor", array('class' => 'required')) ?></td>
            <td colspan="3">
                <?php echo $form->input("UserSupplier.id"); ?>
                <?php echo $form->select("UserSupplier.supplier_id", $suppliers) ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('UserSupplier.read_only', 'Read only'); ?></td>
            <td width="100px">
                <?php echo $form->checkbox('UserSupplier.read_only'); ?> Yes/No
            </td>
            <td width="100px"><?php echo $form->label(null, 'Reporting'); ?></td>
            <td>
                <?php echo $form->checkbox('UserSupplier.supplier_report'); ?> Yes/No
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('UserSupplier.read_message', 'Read Message'); ?></td>
            <td colspan="3">
                <?php echo $form->checkbox('UserSupplier.read_message'); ?> Yes/No
            </td>
        </tr>
    </table>
</fieldset>

<fieldset id="fieldsetCustomer">
    <legend>Customer User Privileges</legend>
    <table>
        <tr>
            <td><?php echo $form->label("UserCustomer.customer_id", "Customer", array('class' => 'required')) ?></td>
            <td colspan="3">
                <?php echo $form->input("UserCustomer.id"); ?>
                <?php echo $form->select("UserCustomer.customer_id", $customers) ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label("UserCustomer.site_id", 'Site', array('id' => 'lblUserSite')) ?></td>
            <td colspan="3">
                <?php echo $form->select("UserCustomer.site_id", $sites) ?>
                <span class="caption">Leave blank for Head Office user</span>
            </td>
        </tr>
        <tr>
            <td width="170px"><?php echo $form->label(null, 'Head Office'); ?></td>
            <td width="150px">
                <span id="headCheckBox">
                    <?php echo $form->checkbox('UserCustomer.is_head_user'); ?> Yes/No
                </span>
                <span id="noHeadUser" style="display: none;">No</span>
            </td>
            <td width="180px"><?php echo $form->label(null, 'View Other Sites'); ?></td>
            <td>
                <span id="viewOtherSite">
                    <?php echo $form->checkbox('UserCustomer.can_view_other_site'); ?> Yes/No
                </span>
                <span id="noView" style="display: none;">No</span>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label(null, 'Read Only'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.read_only'); ?> Yes/No
            </td>
            <td><?php echo $form->label(null, 'Hide Price'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.hide_price'); ?> Yes/No
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label(null, 'Can Add/Change Location'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.can_add_location'); ?> Yes/No
            </td>
            <td><?php echo $form->label(null, 'Read Message'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.read_message'); ?> Yes/No
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label(null, 'Reporting'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.cust_report'); ?> Yes/No
            </td>
            <td><?php echo $form->label(null, 'Reserve Product'); ?></td>
            <td>
                <?php echo $form->checkbox('UserCustomer.can_reserve_stock_out'); ?> Yes/No
            </td>
        </tr>
    </table>
</fieldset>
<?php echo $form->end(); ?>

<fieldset id="fieldsetUserCoH">
    <legend>Customers on Hand</legend>
    <div id="divUserCoH">
        <?php if ($mode == 'add') : ?>
        <span class="caption">Please save user information first.</span>
        <?php else : ?>
            <?php if ($this->data['User']['prefix'] == ITS_PREFIX || $this->data['User']['prefix'] == SUPPLIER_PREFIX) : // ถ้า User คือ ITS หรือ Vendor เห็น Customer on Hand ด้วย?>
                <?php echo $this->requestAction('its_users/customer_on_hand_view/' . $this->data['User']['id'], array('return')); ?>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</fieldset>

<?php if ($mode == 'edit') : ?>
<div id="userHistory">
        <?php echo $this->requestAction("/users/history_display/{$this->data['User']['id']}", array('return')); ?>
</div>
<?php endif; ?>

<script type="text/javascript">
    $j(function() {
        $j("#UserBirthDate").datepicker($j.extend(datepicker_options, {
            yearRange: '-80:0',
            appendText: '<span class="caption"> (dd/mm/yyyy)</span>',
            minDate: false
        }));

        $j(":radio[id^=UserPrefix]").bind("click", function() {
            $j(this).change();
        });
        $j(":radio[id^=UserPrefix]").bind("change", function() {
            var user_type = $j(':checked').val();
            // ซ่อนทั้งหมด แล้วแสดงอันที่เลือก
            $j('#fieldsetIts, #fieldsetCustomer, #fieldsetSupplier, #fieldsetUserCoH').hide();
            switch (user_type) {
                case 'its': $j('#fieldsetIts, #fieldsetUserCoH').show(); break;
                case 'customer': $j('#fieldsetCustomer').show(); break;
                case 'supplier': $j('#fieldsetSupplier, #fieldsetUserCoH').show(); break;
            }
        }).change();

        $j("#UserCustomerCustomerId").bind("change", function() {
            if ($j(this).val() != '') {
                requestOptions("sites", "UserCustomerCustomerId", "UserCustomerSiteId");
                requestJSON('customers', $j("#UserCustomerCustomerId").val(), function(json) {
                    if (json.Customer.have_head_user == true) { // ถ้า customer นี้มี head office user ได้
                        $j("#headCheckBox").show();
                        $j("#noHeadUser").hide();
                    } else {
                        $j("#headCheckBox").hide();
                        $j("#noHeadUser").show();
                    }
                    if (json.Customer.can_view_other_site == true) {
                        $j("#viewOtherSite").show();
                        $j("#noView").hide();
                    } else {
                        $j("#viewOtherSite").hide();
                        $j("#noView").show();
                    }
                });
            }
        });

        $j("#UserCustomerSiteId").bind("change", function() {
            if ($j(this).val() != '') {
                requestJSON('sites', $j("#UserCustomerSiteId").val(), function(json) {
                    if (json.Site.can_reserve_stock_out == true) { // ถ้า Site นี้ สามารถมีการ Reserve Product ได้
                        $j("#UserCustomerCanReserveStockOut").removeAttr('disabled');
                    } else {
                        $j("#UserCustomerCanReserveStockOut").attr('disabled', 'disabled');
                    }
                });
            }
        }).change();

        $j("#UserCustomerIsHeadUser").bind("change", function() {
            if (this.checked == true) {
                $j("#UserCustomerSiteId").attr('disabled', true);
            } else {
                $j("#UserCustomerSiteId").removeAttr('disabled');

            }
        });
    })
</script>
<?php //debug($this->data); ?>
